package com.itheima.media.controller;


import com.itheima.common.pojo.Result;
import com.itheima.common.util.RequestContext;
import com.itheima.core.controller.AbstractCoreController;
import com.itheima.media.pojo.WmMaterial;
import com.itheima.media.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
* <p>
* 自媒体图文素材信息表 控制器</p>
* @author 作者
* @since 2021-08-20
*/
@RestController
@RequestMapping("/wmMaterial")
public class WmMaterialController extends AbstractCoreController<WmMaterial> {

    private WmMaterialService wmMaterialService;

    //注入
    @Autowired
    public WmMaterialController(WmMaterialService wmMaterialService) {
        super(wmMaterialService);
        this.wmMaterialService=wmMaterialService;
    }

    /***
     * 素材保存
     */
    @Override
    @PostMapping
    public Result insert(@RequestBody WmMaterial wmMaterial){
        //获取用户id(当前登录用户的ID)
        //Integer userId=1;
        wmMaterial.setUserId(RequestContext.getNodeValue("id"));
        wmMaterial.setCreatedTime(LocalDateTime.now());
        wmMaterialService.save(wmMaterial);
        return Result.ok();
    }


    /***
     * 修改自媒体文件
     */
    @Override
    @PutMapping
    public Result updateByPrimaryKey(@RequestBody WmMaterial wmMaterial){
        wmMaterialService.updateByIdAndDelFile(wmMaterial);
        return Result.ok();
    }
}

